import { createRouter, createWebHistory } from 'vue-router';
import { get as getToken } from "@/api/JwtApi";

const routes = [
    {
        name: 'index',
        path: '/index',
        component: () => import('@/components/index.vue'),
        children: [
            {
                name: 'admin',
                path: 'admin',
                component: () => import('@/components/admin.vue'),
            },
            {
              name:"member",
              path:"member",
              component:()=>import("@/components/member.vue")
            },
            {
                name:"adminGroup",
                path:"adminGroup",
                component:()=>import("@/components/adminGroup.vue")
            },
            {
                name:"perm",
                path:"perm",
                component:()=>import("@/components/Perm.vue")
            },
            {
                name:"resource",
                path:"resource",
                component:()=>import("@/components/resource.vue")
            },
            {
                name:"brand",
                path:"brand",
                component:()=>import("@/components/brand.vue")
            },
            {
                name:"categories",
                path:"categories",
                component:()=>import("@/components/itemCategory.vue")
            },
            {
                name:"item",
                path:"item",
                component:()=>import("@/components/item.vue")
            },
            {
                name:"order",
                path:"order",
                component:()=>import("@/components/order.vue")
            },
            {
                name:"orderDetail",
                path:"order/detail/:id",
                component:()=>import("@/components/orderDetail.vue")
            },
            {
                name:"view",
                path:"view",  // 修正为相对路径
                component:()=>import('@/components/View.vue')
            },{
                name:"role",
                path:"role",
                component:()=>import("@/components/Role.vue")
            }
        ],
    },
    {
        name: "register",
        path: "/register",
        component: () => import("@/components/Register.vue")
      },
    {
        name: "login",
        path: "/login",
        component: () => import('@/components/login.vue')
    },{
        name:"",
        path: "/",
        redirect: "/index"
    }
];

const router = createRouter({
    history: createWebHistory(),
    routes,
});

// 路由守卫
// router.beforeEach((to, from, next) => {
//     const jwt = getToken();

//     if (jwt) { // 已登录
//         if (to.name === "login") {
//             next({ name: "index" });  // 登录后不能再去登录页
//         } else {
//             next(); // 正常访问其他页面
//         }
//     } else {  // 未登录
//         if (to.name !== "login") {
//             // 未登录访问非登录页，重定向到登录页，同时带上redirect查询参数
//             next({ name: "login", query: { redirect: to.fullPath } });
//         } else {
//             next(); // 正常访问登录页
//         }
//     }
// });
const whiteList = ["login", "register"];

router.beforeEach((to, from, next) => {
    // 暂时注释掉登录检查，方便测试
    // const jwt = getToken();

    // if (jwt) {
    //     if (to.name === "login") {
    //         next({ name: "index" });  // 登录后不能再去登录页
    //     } else {
    //         next();
    //     }
    // } else {
    //     if (whiteList.includes(to.name)) {
    //         next(); // 白名单页面放行（如 login、register）
    //     } else {
    //         next({ name: "login", query: { redirect: to.fullPath } });
    //     }
    // }
    
    // 允许无条件访问所有页面
    next();
});
export default router;